/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */

package com.nai365.nart.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.log.annotation.SysLog;
import com.nai365.nart.product.entity.Brand;
import com.nai365.nart.product.entity.CategoryBrandRelation;
import com.nai365.nart.product.service.CategoryBrandRelationService;
import com.nai365.nart.product.vo.BrandVo;
import org.springframework.security.access.prepost.PreAuthorize;
import com.nai365.nart.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 品牌分类关联
 *
 * @author baogs
 * @date 2022-07-01 11:05:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/categorybrandrelation" )
@Api(value = "categorybrandrelation", tags = "品牌分类关联管理")
public class CategoryBrandRelationController {

    private final  CategoryBrandRelationService categoryBrandRelationService;


	/**
	 * 获取当前品牌关联的所有分类列表列表
	 */
	@GetMapping(value = "/catelog/list")
	public R catelogList(@RequestParam Map<String, Object> params, @RequestParam("brandId") Long brandId){

		List<CategoryBrandRelation> data = categoryBrandRelationService.
				list(new QueryWrapper<CategoryBrandRelation>().eq("brand_id",brandId));

		return R.ok(data);
	}

	/**
	 * /product/categorybrandrelation/brands/list
	 * 1、Controller：处理请求，接收和效验数据
	 * 2、Service接收Controller传来的数据，进行业务处理
	 * 3、Controller接收Service处理完的数据，封装页面指定的vo
	 */
	@GetMapping(value = "/brands/list")
	public R relationBransList(@RequestParam(value = "catId",required = true) Long catId) {

		List<Brand> vos = categoryBrandRelationService.getBrandsByCatId(catId);

		List<BrandVo> collect = vos.stream().filter(Objects::nonNull).map(item -> {
			BrandVo brandVo = new BrandVo();
			brandVo.setBrandId(item.getBrandId());
			brandVo.setBrandName(item.getName());
			return brandVo;
		}).collect(Collectors.toList());

		BrandVo brandVo = new BrandVo();
		brandVo.setBrandId(140L);
		brandVo.setBrandName("纯链科技");
		collect.add(brandVo);

		//去重
		ArrayList<BrandVo> collect1 = collect.stream().collect(
				Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<BrandVo>(Comparator.comparing(item -> item.getBrandId()))), ArrayList::new));
		return R.ok(collect1);
	}


	/**
     * 分页查询
     * @param page 分页对象
     * @param categoryBrandRelation 品牌分类关联
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getCategoryBrandRelationPage(Page page, CategoryBrandRelation categoryBrandRelation) {
        return R.ok(categoryBrandRelationService.page(page, Wrappers.query(categoryBrandRelation)));
    }


    /**
     * 通过id查询品牌分类关联
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(categoryBrandRelationService.getById(id));
    }

    /**
     * 新增品牌分类关联
     * @param categoryBrandRelation 品牌分类关联
     * @return R
     */
    @ApiOperation(value = "新增品牌分类关联", notes = "新增品牌分类关联")
    @SysLog("新增品牌分类关联" )
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandRelation categoryBrandRelation) {
        return R.ok(categoryBrandRelationService.save(categoryBrandRelation));
    }

    /**
     * 修改品牌分类关联
     * @param categoryBrandRelation 品牌分类关联
     * @return R
     */
    @ApiOperation(value = "修改品牌分类关联", notes = "修改品牌分类关联")
    @SysLog("修改品牌分类关联" )
    @PutMapping
    public R updateById(@RequestBody CategoryBrandRelation categoryBrandRelation) {
        return R.ok(categoryBrandRelationService.updateById(categoryBrandRelation));
    }

    /**
     * 通过id删除品牌分类关联
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除品牌分类关联", notes = "通过id删除品牌分类关联")
    @SysLog("通过id删除品牌分类关联" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Long id) {
        return R.ok(categoryBrandRelationService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param categoryBrandRelation 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CategoryBrandRelation> export(CategoryBrandRelation categoryBrandRelation) {
        return categoryBrandRelationService.list(Wrappers.query(categoryBrandRelation));
    }


	/**
	 * 删除
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] ids){
		return R.ok(categoryBrandRelationService.removeByIds(Arrays.asList(ids)));
	}
}
